package com.ds.springdata8jpa.controller;

import com.ds.springdata8jpa.pojo.User;
import com.ds.springdata8jpa.repository.UserRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: SpringBoot
 * @description:
 * @author: Dawson.Zhang
 * @create: 2020-05-14
 **/
@RestController
public class UserController {
    @Resource
    UserRepository userRepository;
    @GetMapping("/users")
    public List<User> getUers(){
        List<User> all = userRepository.findAll();
        return all;
    }
    @PostMapping("/user")
    public User addUser(User user){
        User save = userRepository.save(user);
        return save;
    }
    @GetMapping("/user/{id}")
    public User getById(@PathVariable("id")Integer id){
        User user = userRepository.findById(id).get();
        return user;
    }
}
